﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Item;
using System.Threading.Tasks;
using LitJson;
using Player;

public static class Editor_Menu 
{
    public struct Test_A
    {
        public int aaa;
    }
    public struct Test_B
    {
        public int bbb;
    }

    private static bool bCanInitItemData = true;
    [MenuItem("MyMenu/TBL/初始化数据表(只添加新数据)")]
    public async static void InitItemDataTBLAsync()
    {
        await InitItemDataTBLAsync(false);
    }
    [MenuItem("MyMenu/TBL/初始化数据表(重写)")]
    public async static void InitItemDataTBLAsyncRewrite()
    {
        await InitItemDataTBLAsync(true);
    }

    private async static Task InitItemDataTBLAsync(bool rewrite)
    {
        if (!bCanInitItemData) return;
        bCanInitItemData = false;

        Debug.Log("-----------------");

        if (!rewrite)
        {
            Debug.Log("开始加载-道具-信息表");
            await ItemDataMgr.Ins.LoadTBLDataAsync();
            Debug.Log("加载-道具-信息表完毕");
        }

        try
        {
            STItemData[] arr = Editor_DebugItemData.GetDebugItemData();

            foreach (var item in arr)
            {
                if (!ItemDataMgr.Ins[item.strItemTag].HasValue)
                {
                    ItemDataMgr.Ins[item.strItemTag] = item;
                }
            }

            Debug.Log("开始保存新的-道具-信息表");
            ItemDataMgr.Ins.SaveTBLData();
            Debug.Log("-道具-信息表保存完毕");

            Debug.Log("开始保存新的-科研-信息表");
            Science.SaveTBLData();
            Debug.Log("-科研-信息表保存完毕");

            Debug.Log("开始保存新的-商店-信息表");
            Shop.SaveTBLData();
            Debug.Log("-商店-信息表保存完毕");

            AssetDatabase.Refresh();
        }
        catch (System.Exception e)
        {
            throw e;
        }

        Debug.Log("-----------------");

        bCanInitItemData = true;
    }
}
